package cn.lj.controller.admin;

import cn.lj.pojo.Type;
import cn.lj.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author lj
 * @Description:
 * @date 2020-11-27 13:39
 * @QQ 851477108
 */

@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private TypeService typeService;

    /**
     * 分页查询所有类型
     * @param pagenum
     * @param model
     * @return
     */
    @GetMapping("/types")
    public String types(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum,8);
        List<Type> allType = typeService.getAllType();
        //得到分页结果对象
        PageInfo<Type> pageInfo = new PageInfo<>(allType);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/types";
    }

    /**
     * 跳转新增页,返回一个type对象给前端th:object
     * @param model
     * @return
     */
    @GetMapping("/types/input")
    public String toAddType(Model model){
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    /**
     * 新增类型
     * @param type
     * @param attributes
     * @return
     */
    @PostMapping("/types")
    public String addType(Type type, RedirectAttributes attributes){
        Type t = typeService.getTypeByName(type.getName());
        if (t!=null){
            attributes.addFlashAttribute("msg","不能添加重复的分类");
            return "redirect:/admin/types/input";
        }else {
            attributes.addFlashAttribute("msg","添加成功");
        }
        typeService.saveType(type);
        return "redirect:/admin/types";   //不能直接跳转到types页面，否则不会显示type数据(没经过types方法)
    }

    /**
     * 跳转修改页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}/input")
    public String toEditType(@PathVariable Long id, Model model){
        model.addAttribute("type", typeService.getTypeById(id));
        return "admin/types-input";
    }

    /**
     * 根据id修改types
     * @param type
     * @return
     */
    @PostMapping("/types/{id}")
    public String editType(Type type, RedirectAttributes attributes){
        Type t = typeService.getTypeByName(type.getName());
        if (t!=null){
            attributes.addFlashAttribute("msg","不能修改重复的分类");
            return "redirect:/admin/types/input";
        }else {
            attributes.addFlashAttribute("msg","修改成功");
        }
        typeService.updateType(type);
        return "redirect:/admin/types";   //不能直接跳转到types页面，否则不会显示type数据(没经过types方法)
    }

    /**
     * 删除
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("/types/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        typeService.deleteType(id);
        attributes.addFlashAttribute("msg","删除成功");
        return "redirect:/admin/types";
    }
}
